﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using System.IO;
using GreatB2C.Utility;
using GreatB2C.Business;
using GreatB2C.Config.Shop;

namespace GreatB2C.Web.Manage.SystemSet.Contents
{
    public partial class Model : Inherit.ManagePage
    {
        /// <summary>
        /// 图标相对路径
        /// </summary>
        const string IconPath = "/Resource/Images/ModelIcon";

        /// <summary>
        /// 默认字段信息模板
        /// </summary>
        const string DefaultFields = "/Config/Sytem/Shop/DefaultTemplateFields.xml";

        /// <summary>
        /// 业务操作类
        /// </summary>
        Business.Model BLL = new Business.Model( );

        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                ddlModelTemplate.Items.Clear( );
                Business.ModelTemplates bllTemp = new Business.ModelTemplates( );
                DataTable dtType = bllTemp.ListModelTemplates( false );
                if ( dtType != null && dtType.Rows.Count > 0 )
                {
                    ddlModelTemplate.DataSource = dtType;
                    ddlModelTemplate.DataTextField = "TemplateName";
                    ddlModelTemplate.DataValueField = "TemplateID";
                    ddlModelTemplate.DataBind( );
                }
                ddlModelTemplate.Items.Insert( 0 , new ListItem( "空白内容模型模板" , "" ) );

                ddlItemIcon.Items.Clear( );
                DirectoryInfo dir = new DirectoryInfo( Server.MapPath( IconPath ) );
                FileInfo[] files = dir.GetFiles( );
                if ( files != null && files.Length > 0 )
                {
                    foreach ( FileInfo file in files )
                    {
                        ListItem li = new ListItem( );
                        li.Text = file.Name;
                        li.Value = file.Name;

                        if ( file.Name.ToString( ).ToLower( ) == "default.gif" )
                        {
                            li.Selected = true;
                        }

                        ddlItemIcon.Items.Add( li );
                    }
                }


                //Business.Dictionary dictBLL = new Business.Dictionary( );
                //Model.Dictionary dictModel = dictBLL.ListByFieldName( DataDefine.Dictionary.ModelType.ToString( ) );

                //if ( dictModel != null )
                //{
                //    if ( dictModel.FieldValue.Trim( ) != "" )
                //    {
                //        // 格式 ： 选项名|是否启用值|默认值
                //        int i=0;
                //        string[] parts = dictModel.FieldValue.ToString( ).Split( '$' );
                //        foreach ( string part in parts )
                //        {
                //            if ( part.IndexOf( "|" ) > 0 )
                //            {
                //                string[] choises  = part.Split( '|' );
                //                if ( choises.Length == 3 )
                //                {
                //                    radlModelType.Items.Add( new ListItem( choises[ 0 ] , i.ToString( ) ) );
                //                    i++;
                //                }
                //            }
                //        }
                //    }
                //}

                //if ( radlModelType.Items.Count > 0 )
                //    radlModelType.Items[ 0 ].Selected = true;

                //Business.Producer bllProd = new Producer( );
                //DataTable dtProducer = bllProd.ListProducerByPassed( true );
                //if ( dtProducer != null && dtProducer.Rows.Count > 0 )
                //{
                //    ddlProducerID.DataSource = dtProducer;
                //    ddlProducerID.DataTextField = "ProducerName";
                //    ddlProducerID.DataValueField = "ProducerID";
                //    ddlProducerID.DataBind( );
                //}

                //if ( Request.QueryString[ "ProducerId" ] != null && Request.QueryString[ "ProducerId" ].ToString( ) != "" )
                //{
                //    ddlProducerID.SelectedValue = Request.QueryString[ "ProducerId" ].ToString( );
                //    ddlProducerID.Enabled = false;
                //}
            }
        }

        protected void BtnSubmit_Click( object sender , EventArgs e )
        {
            GreatB2C.Model.Model model = new GreatB2C.Model.Model( );
            model.ModelID = -1;
            model.ModelName = txtModelName.Text.Trim( );
            model.ModelType = 1;
            model.Description = txtDescription.Text.Trim( );
            model.ModelName = txtModelName.Text.Trim( );
            model.TableName = lblTablePrefix.Text.Trim( ) + txtTableName.Text.Trim( );
            model.ItemName = txtItemName.Text.Trim( );
            model.ItemUnit = txtItemUnit.Text.Trim( );
            model.ItemIcon = txtItemIcon.Text.Trim( );
            model.IsCountHits = true;
            model.Disabled = false;

            string abspath = Server.MapPath( DefaultFields );
            if ( System.IO.File.Exists( abspath ) )
            {
                StreamReader sr = new StreamReader( abspath , Encoding.UTF8 );
                model.Field = sr.ReadToEnd( );
            }

            model.DefaultTemplateFile = ucContentTemplate.TemplatePath;
            model.EnableCharge = Convert.ToBoolean( radEnableCharge.SelectedValue );
            model.EnableSignin = Convert.ToBoolean( radEnableSignin.SelectedValue );
            model.AddInfoFilePath = txtAddInfoFilePath.Text.Trim( );
            model.ManageInfoFilePath = txtManageInfoFilePath.Text.Trim( );
            model.PreviewInfoFilePath = txtPreviewInfoFilePath.Text.Trim( );
            model.BatchInfoFilePath = txtBatchInfoFilePath.Text.Trim( );
            model.Character = 0;
            model.MaxPerUser = 0;
            model.PrintTemplate = ucPrintTemplate.TemplatePath;
            model.EnableVote = Convert.ToBoolean( radVote.SelectedValue );
            model.SearchTemplate = ucSearchTemplate.TemplatePath;
            model.AdvanceSearchFormTemplate = ucAdvanceSearchFormTemplate.TemplatePath;
            model.AdvanceSearchTemplate = ucAdvanceSearchTemplate.TemplatePath;
            model.ChargeTips = txtModelChargeTips.Text.Trim( );
            model.NeedPointChargeTips = txtNeedPointChargeTips.Text.Trim( );
            model.OutTimeChargeTips = txtOutTimeChargeTips.Text.Trim( );
            model.UsePointChargeTips = txtUsePointChargeTips.Text.Trim( );
            model.CommentManageTemplate = ucCommentManageTemplate.TemplatePath;
            model.AnonymouseTemplate = ucAnonymouseAddContentTemplate.TemplatePath;
            model.UserAddContentTemplate = ucUserAddContentTemplate.TemplatePath;
            model.EnableVerificationCode = Convert.ToBoolean( radEnableVerificationCode.SelectedValue );
            model.EnableWap = Convert.ToBoolean( radEnableWap.SelectedValue );

            string wapField = "";

            if ( Request.QueryString[ "Action" ] == "Modify" )
            {
                model.ModelID = Convert.ToInt32( Request.QueryString[ "ModelID" ].ToString( ) );
                wapField = ddlTitle.SelectedValue + "||," + ddlContent.SelectedValue + "||," + ddlAuthor.SelectedValue + "||,"  + ddlStatus.SelectedValue + "||";
            }
            model.WapField = wapField;

            bool isExist = BLL.IsExists( model );
            if ( isExist )
            {
                litScript.Text = "<script type='text/javascript'>ShowError('错误','该模板信息已经存在，请重新输入！');</script>";
                return;
            }
            else
            {
                // 根据Template指定模型生成表结构 ddlModelTemplate
                if ( ddlModelTemplate.SelectedValue != "" )
                {
                    GreatB2C.Model.ModelTemplates temp = new GreatB2C.Model.ModelTemplates( );
                    temp.TemplateID = Convert.ToInt32( ddlModelTemplate.SelectedValue );
                    Business.ModelTemplates mtBLL = new ModelTemplates( );
                    temp = mtBLL.ListByID( temp );
                    if ( temp != null )
                    {
                        model.Field = temp.Field;
                    }
                }

                Component.DefaultTemplateFields dtf = new Component.DefaultTemplateFields( );
                dtf.Field = model.Field;
                DataTable dtFields = dtf.GetDataTable( "1" );

                bool isSuccess = BLL.Insert( model , dtFields );
                if ( isSuccess )
                {
                    litScript.Text = "<script type='text/javascript'>ShowInfoAndRedirect('操作成功','模板信息添加成功！','ModelManage.aspx');</script>";
                    return;
                }
                else
                {
                    litScript.Text = "<script type='text/javascript'>ShowError('操作失败','模板信息添加失败！');</script>";
                    return;
                }
            }
        }

    }
}
